package net.butfly.bus.filter;

import java.util.Map;

import net.butfly.albacore.exception.SystemException;
import net.butfly.bus.Constants;
import net.butfly.bus.Request;
import net.butfly.bus.Response;
import net.butfly.bus.util.XMLUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimulateFilter extends FilterBase implements Filter {
	private static final Logger logger = LoggerFactory.getLogger(SimulateFilter.class);
	private String source;

	@Override
	public void initialize(Map<String, String> params) {
		this.source = params.get("source");
		if (null == this.source)
			throw new SystemException(Constants.UserError.CONFIG_ERROR, "系统配置为模拟器模式(simulate)，但是没有配置模拟报文文件路径（simulate-dir）.");
		else
			logger.info("EBox存放模拟报文文件的路径为 " + source);
	}

	@Override
	public void execute(Request request, Response response) {
		String filename = request.code() + "-" + request.version() + ".xml";
		Object r;
		try {
			r = XMLUtils.parse(source + (source.endsWith("\\") ? "" : "\\") + filename);
		} catch (Throwable e) {
			throw new SystemException(Constants.BusinessError.SIMULATE_ERROR, e);
		}
		response.setResult(r);
	}
}
